package com.eclink.asset.consume.controller;

import com.eclink.asset.consume.domain.dao.ConsumeInventoryCheck;
import com.eclink.asset.consume.domain.dao.ConsumeInventoryCheckRelItem;
import com.eclink.asset.consume.domain.query.ConsumeInventoryCheckAddQuery;
import com.eclink.asset.consume.domain.query.ConsumeInventoryCheckInventoryQuery;
import com.eclink.asset.consume.service.ConsumeCategoryService;
import com.eclink.asset.consume.service.ConsumeInventoryCheckDetailService;
import com.eclink.asset.consume.service.ConsumeInventoryCheckService;
import com.eclink.asset.domain.dto.DistributeItem;
import com.eclink.asset.domain.query.AssetInventoryQuery;

import com.eclink.asset.service.CommonUserService;
import com.ruoyi.common.core.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 耗材Controller
 *
 * @author chenshixian
 * @date 2024-08-26
 */
@Api(tags ="耗材盘点接口")
@RestController
@RequestMapping("/consume/inventoryCheck")
public class ConsumeInventoryCheckController extends BaseController {

    @Resource
    private ConsumeInventoryCheckService service;

    @Resource
    private ConsumeInventoryCheckDetailService detailService;

    @Resource
    private CommonUserService commonUserService;

    @Resource
    private ConsumeCategoryService consumeCategoryService;

    @ApiOperation("查询列表")
    @PostMapping("/list")
    public ResponseEntity<Page<ConsumeInventoryCheck>> list(@RequestBody AssetInventoryQuery query, Pageable page) {
        List<ConsumeInventoryCheck> list = service.selectList(query, page);
        for(ConsumeInventoryCheck assetInventory : list){
            if (assetInventory.getUserId()!=null && assetInventory.getUserId() != 0) {
                assetInventory.setUserName(commonUserService.getNickNameById(assetInventory.getUserId()));
            }
        }
        long total = ( (com.github.pagehelper.Page) list ).getTotal();
        return ResponseEntity.ok(new PageImpl<>(list, page, total));
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public ResponseEntity<Integer> save(@RequestBody ConsumeInventoryCheckAddQuery saveDTO) {
        service.save(saveDTO);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("盘点")
    @PostMapping("/inventory")
    public ResponseEntity<Integer> inventory(@RequestBody ConsumeInventoryCheckInventoryQuery query) {
        detailService.inventory(query);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("详情")
    @GetMapping("/{id}")
    public ResponseEntity<ConsumeInventoryCheck> detail(@PathVariable String id) {
        return ResponseEntity.ok(service.getDetailById(id));
    }

    @ApiOperation("查询列表")
    @PostMapping("/detail/list/{parentId}")
    public ResponseEntity<Page<ConsumeInventoryCheckRelItem>> detailList(@RequestBody AssetInventoryQuery query, Pageable page, @PathVariable String parentId) {
        List<ConsumeInventoryCheckRelItem> list = detailService.selectListDetailItem(query, page, parentId);
        for(ConsumeInventoryCheckRelItem item : list){
            if (item.getCategoryId()!=null && item.getCategoryId() != 0) {
                item.setCategoryName(consumeCategoryService.selectNameById(item.getCategoryId()));
            }

        }
        long total = ( (com.github.pagehelper.Page) list ).getTotal();
        return ResponseEntity.ok(new PageImpl<>(list, page, total));
    }



    @ApiOperation("提交盘点结果前的提示")
    @GetMapping("/getTipContent/{id}")
    public ResponseEntity<List<DistributeItem>> getTipContent(@PathVariable String id) {
        return ResponseEntity.ok(service.getTipContent(id));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        service.deleteByIds(ids);
        return ResponseEntity.ok(1);
    }

}
